package com.example.administrator.meishi.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.example.administrator.meishi.R;
import com.example.administrator.meishi.tools.NewsDao;

public class NewActivity extends AppCompatActivity {
    private VideoView vv;
    private TextView tx;
    private String text;
    private String src;
    private NewsDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        initView();
        initData();
        dao = new NewsDao(this);
    }

    private void initData() {
        //得到video的地址和video详情
        Intent intent = getIntent();
        src = intent.getStringExtra("video");
        text = intent.getStringExtra("text");
        //设置video播放器
        vv.setMediaController(new MediaController(this));
        vv.setVideoURI(Uri.parse(src));
        vv.requestFocus();
        vv.start();
        //设置详情
        tx.setText(text);
    }

    private void initView() {
        vv = (VideoView) findViewById(R.id.vv);
        tx = (TextView) findViewById(R.id.new_text);

    }
    //收藏功能
    public void collect(View view) {

        String sql = "select * from shen where test = ?";
        Cursor cursor = dao.select(sql, new String[]{text});
        int i = 0;
        while (cursor.moveToNext()) {
            i++;
        }
        cursor.close();
        if(i ==0){
            ContentValues values = new ContentValues();
            values.put("test", text);
            values.put("path",src);
            long insert = dao.insert("shen", values);
            if (insert > 0) {
                Toast.makeText(this, "收藏成功!", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "收藏失败", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(this, "已收藏", Toast.LENGTH_SHORT).show();
        }
    }

}
